package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.customercommunicate.pojo.dto.DynamicListDTO;
import com.xbongbong.pro.customercommunicate.pojo.vo.CommunicateDynamicsListVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formdata.pojo.dto.FormDataDetailDTO;
import com.xbongbong.pro.formdata.pojo.vo.FormDataDetailVO;
import com.xbongbong.saas.service.SaasDynamicService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 *
 * date: 2022/5/20 3:23 下午
 * @author yingjf
 * 动态详情接口
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/dynamic")
public class DynamicController {

    @Resource
    private SaasDynamicService saasDynamicService;

    /**
     * 动态列表
     * @author lzf
     * @date 2022/4/14 16:10
     * @param dynamicListDTO
     * @param br
     * @return String
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getDynamicsList(@RequestBody @Valid DynamicListDTO dynamicListDTO, BindingResult br) {
        XbbResponse<CommunicateDynamicsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CommunicateDynamicsListVO communicateDynamicsListVO = saasDynamicService.getDynamicsList(dynamicListDTO);
                response = new XbbResponse<>(communicateDynamicsListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 简单动态列表
     * @param dynamicListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/listOfSimple", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSimpleDynamicsList(@RequestBody @Valid DynamicListDTO dynamicListDTO, BindingResult br) {
        XbbResponse<CommunicateDynamicsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CommunicateDynamicsListVO communicateDynamicsListVO = saasDynamicService.getSimpleDynamicList(dynamicListDTO);
                response = new XbbResponse<>(communicateDynamicsListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid FormDataDetailDTO formDataDetailDTO, BindingResult br) {
        XbbResponse<FormDataDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(formDataDetailDTO.getSaasMark(),formDataDetailDTO.getBusinessType(), formDataDetailDTO.getMenuId(), formDataDetailDTO.getDistributorMark());
                FormDataDetailVO formDataDetailVO = saasDynamicService.getDetail(formDataDetailDTO);
                response = new XbbResponse<>(formDataDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
